#include <stdio.h>
#include <stdlib.h>  
#include <string.h>
 
#if !defined(LOCK_SH) 
    #define LOCK_SH     1   /* shared lock */
#endif
#if !defined(LOCK_EX)
    #define LOCK_EX     2   /* exclusive lock */
#endif
#if !defined(LOCK_NB)
    #define LOCK_NB     4   /* don't block when locking */
#endif
#if !defined(LOCK_UN)
    #define LOCK_UN     8   /* unlock */
#endif
int lock_shm(const char*shmname,int op);
 
void dump(char *prog) 
{
    char buf[80]; 
    sprintf(buf,"dump[%s].",prog);  
    write(1, buf, strlen(buf)); 
    sleep(1); 
} 
 
void main(int argc, char* argv[]) 
{ 
    char  shmname[] = "TEST_BCACHE"; 
    int i; 
    char *prog; 
    prog = argv[1]; 
    printf("[%s]LOCK_EX:%d\n",  prog, lock_shm(shmname,LOCK_EX) ); 
    for(i = 0; i < 5; i++) {
        dump(prog); 
    }

    printf("\n[%s]LOCK_UN:%d\n",  prog, lock_shm(shmname,LOCK_UN) ); 
    printf("[%s]LOCK_SH:%d\n", prog, lock_shm(shmname,LOCK_SH)  ); 
    for(i =0; i < 5; i++) {
        dump(prog); 
    }

    printf("\n[%s]LOCK_UN:%d\n", prog, lock_shm(shmname,LOCK_UN)  ); 
    printf("[%s]LOCK_EX:%d\n", prog, lock_shm(shmname,LOCK_EX) ); 
    for(i = 0; i < 5;  i++) {
        dump(prog); 
    }

    printf("\n[%s]LOCK_UN:%d\n", prog, lock_shm(shmname,LOCK_UN)  ); 
}
